define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;

    var emotion = require('./emotion.js');

    var textItem = require("./items/textItem.html");
    var textBlockTpl = require("./blocks/textBlockTpl.html");

    var textcompose = require('./composes/composeTextView.html');
    var textview = require("./preview/textView.html");
    var textfooter = require("./footers/textfooter.html")

    var emptyHtml = require('./emptys/word.html')
    var textfiles = function(element, layout, generate) {
        this.element = $(element);
        this.selector = ".js_text_container li.item:last";
        this.type = window.message_config.files_type_ids[0];
        this.layout = layout;
        this.init();
        generate && this.generate();
    }
    textfiles.prototype.init = function() {
        this.element.empty();
        this.events()
        this.selectView();
        this.fileModel = {
            Text: {
                textContent: ''
            }
        }
        this.model = {}
    };
    textfiles.prototype.templateData = function() {
        return {
            model: this.model,
            textContent: this.fileModel.Text.textContent,
            app: window.message_config.app
        }
    };
    textfiles.prototype.events = function() {
        var _self = this;
        $(document).off('click.js_text_container').on("click.js_text_container", ".js_text_container li.item", function() {
            var $this = $(this),
                $fileId = $this.data("fileId");
            $(".js_text_container li.item").removeClass('selected');
            $this.addClass('selected').siblings().removeClass('selected');
            _self.selectData(window.files[_self.type][$fileId]);
        })

    };
    textfiles.prototype.validation = function() {
        if ($.trim(this.fileModel.Text.textContent).length==0) {
            config.msg.info("请输入消息内容")
            return false;
        };
        return true;
    };
    textfiles.prototype.sync = function(data) {
        var _self = this;
        var preview = template.compile(textview)(this.templateData());
        this.layout.showPreview();
        this.layout.setPreview(preview);
    }
    textfiles.prototype.list = function(data) {
        var render_list = template.compile(textBlockTpl);
        var render_item = template.compile(textItem);
        var sp = $('ul:last', this.element);
        if (sp.length) {
            sp.append(render_item({
                list: data.file_item
            }))
        } else {
            if (data.last_file_item.length) {
                this.element.append(render_list({
                    title: "最近",
                    file_items: render_item({
                        list: data.last_file_item
                    })
                }));
            }
            if (data.file_item.length) {
                this.element.append(render_list({
                    title: "全部",
                    file_items: render_item({
                        list: data.file_item
                    })
                }));
            } else {
                this.element.html(emptyHtml)
            }
        }
    };
    textfiles.prototype.selectData = function(data) {
        var _self = this;
        $(".js_add_message").off("click").removeAttr('disabled').bind('click', function(event) {
            _self.generate(data)
        });
    }
    textfiles.prototype.selectView = function() {
        var $footer = $(textfooter);
        var _self = this; 
        $(".js_new_message", $footer).on("click", function() {
            _self.generate();
        })
        this.layout.setFooter($footer);
    }
    textfiles.prototype.sync = function() {
        var _self = this;
        this.fileModel.Text.textContent = _self.$textarea.val();


        var previewdata = $.extend({}, this.templateData(), {
            textContent: this.emotion.htmltranslation(this.fileModel.Text.textContent)
        })

        _self.layout.setPreview(template.compile(textview)(previewdata));

    }
    textfiles.prototype.generateChange = function($warp) {
        var _self = this;
        this.$textarea.on("keyup", function() {
            _self.$num_limit.text(600 - _self.$textarea.val().length - _self.$textarea.val().split("\n").length + 1);
            _self.sync();
        });

    }
    textfiles.prototype.generate = function(data) {
        var _self = this;
        if (data) {
            this.fileModel.Text.textContent = data.Text.content;
        };

        var warp = template.compile(textcompose)(this.templateData());
        var $warp = $(warp);
        this.$textarea = $("textarea", $warp);

        this.$num_limit = $(".js_input_word_num_limit", $warp);
        this.emotion = new emotion($(".js_emoticon", $warp), this.$textarea);


        var previewdata = data ? $.extend({}, this.templateData(), {
            textContent: this.emotion.htmltranslation(data.Text.content)
        }) : this.templateData();

        var preview = template.compile(textview)(previewdata);
        if (data) {
            this.$num_limit.text(600 - data.Text.content.length)
        };

        this.generateChange($warp);
        this.layout.setWarp($warp);
        this.$textarea.focusEnd();
        this.layout.showPreview();
        this.layout.setPreview(preview);
        this.layout.setMassfooter(this);
        this.layout.blcokDestroy()
    }
    return textfiles;

})
